package com.lpf.back.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lpf.back.common.CommonRes;
import com.lpf.back.domain.Activity;
import com.lpf.back.domain.Dynamic;
import com.lpf.back.domain.User;
import com.lpf.back.domain.UserActivity;
import com.lpf.back.domain.vo.ActivityUserVO;
import com.lpf.back.domain.vo.UserActivityVO;
import com.lpf.back.domain.vo.UserDynamicVO;
import com.lpf.back.mapper.ActivityMapper;
import com.lpf.back.mapper.UserActivityMapper;
import com.lpf.back.mapper.UserMapper;
import com.lpf.back.service.interfaces.ActivityService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private UserActivityMapper userActivityMapper;

    @Override
    public List<Activity> getAllActivity() {
        List<Activity> activities = activityMapper.selectList(null);
        for (Activity activity : activities) {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            activity.setTemp1(format.format(activity.getReleaseTime()));

            if(activity.getActiveStatus() == 0){
                activity.setTemp2("已结束");
            } else if (activity.getActiveStatus() == 1){
                activity.setTemp2("进行中");
            } else {
                activity.setTemp2("敬请期待");
            }
        }
        return activities;
    }

    @Override
    public int releaseActivity(Activity activity) {
        int insert = activityMapper.insert(activity);
        return insert;
    }

    @Override
    public Activity getActivity(Integer id) {
        return activityMapper.selectById(id);
    }

    @Override
    public int updateActivity(HttpServletRequest request, String title, String description, HttpSession session) {
        String status = request.getParameter("status");
        int activityStatus = -1;
        if("敬请期待".equals(status)){
            activityStatus = 2;
        } else if ("进行中".equals(status)){
            activityStatus = 1;
        } else {
            activityStatus = 0;
        }
        Activity activity = (Activity)session.getAttribute("activity");
        activity.setActiveStatus(activityStatus);
        activity.setTitle(title);
        activity.setDescription(description);
        return activityMapper.updateById(activity);
    }

    @Override
    public CommonRes participateActivity(Integer userid, Integer activityid) {
        QueryWrapper<UserActivity> wrapper = new QueryWrapper<>();
        wrapper.eq("userid", userid);
        wrapper.eq("activityid", activityid);
        UserActivity userActivity = userActivityMapper.selectOne(wrapper);
        if (activityMapper.selectById(activityid).getActiveStatus() == 0){
            return new CommonRes(null, -1, "该活动已经结束，无法参加！");
        } else if (activityMapper.selectById(activityid).getActiveStatus() == 2){
            return new CommonRes("null", 2, "该活动还未未开始，敬请期待！");
        } else if (userActivity == null) {
            UserActivity uActivity = new UserActivity();
            uActivity.setId(null);
            uActivity.setUserid(userid);
            uActivity.setActivityid(activityid);
            userActivityMapper.insert(uActivity);
            Activity activity = activityMapper.selectById(activityid);
            activity.setParticipateNum(activity.getParticipateNum()+1);
            activityMapper.updateById(activity);
            return new CommonRes(uActivity, 1, "活动参与成功！");
        } else {
            return new CommonRes(userActivity, 0, "已经参与了该活动，无法重复参加！");
        }
    }

    @Override
    public Activity likeActivity(Integer userid, Integer activityid) {
        Activity activity = activityMapper.selectById(activityid);
        String likeid = activity.getTemp1();
        List<Integer> userIdList = new ArrayList<>();
        if(likeid != null){
            userIdList = JSON.parseArray(likeid, Integer.class);
            if(!userIdList.contains(userid)){
                activity.setLikes(activity.getLikes() + 1);
                userIdList.add(userid);
                String newUserIdList = JSON.toJSONString(userIdList);
                activity.setTemp1(newUserIdList);
                activityMapper.updateById(activity);
                return activity;
            } else {
                return null;
            }
        }
        activity.setLikes(1);
        userIdList.add(userid);
        activity.setTemp1(JSON.toJSONString(userIdList));
        activityMapper.updateById(activity);
        return activity;
    }

    @Override
    public List<UserDynamicVO> getUserList(Integer activityid) {
        List<UserDynamicVO> userDynamicVOS = activityMapper.selectUserActivityVOList(activityid);
        return userDynamicVOS;
    }

    @Override
    public List<ActivityUserVO> getMyActivity(Integer userid) {
        List<ActivityUserVO> activityUserVOS = activityMapper.selectActivityUserVOList(userid);
        for (ActivityUserVO activityUserVO : activityUserVOS) {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            activityUserVO.setTemp1(format.format(activityUserVO.getReleaseTime()));

            if(activityUserVO.getActiveStatus() == 0){
                activityUserVO.setTemp2("已结束");
            } else if (activityUserVO.getActiveStatus() == 1){
                activityUserVO.setTemp2("进行中");
            } else {
                activityUserVO.setTemp2("敬请期待");
            }
        }
        return activityUserVOS;
    }
}
